package rc.work.module.bpm.controller.admin.problem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import rc.work.framework.common.pojo.CommonResult;
import rc.work.module.bpm.controller.admin.oa.vo.BpmOALeaveCreateReqVO;
import rc.work.module.bpm.controller.admin.problem.vo.Edge;
import rc.work.module.bpm.controller.admin.problem.vo.Node;
import rc.work.module.bpm.controller.admin.problem.vo.ProblemBpmRespVO;
import rc.work.module.bpm.controller.admin.problem.vo.ProblemSaveVO;
import rc.work.module.bpm.dal.dataobject.problem.ProblemBpmDO;
import rc.work.module.bpm.dal.dataobject.problem.ProblemGroupDO;
import rc.work.module.bpm.dal.mysql.problem.ProblemBpmMapper;
import rc.work.module.bpm.dal.mysql.problem.ProblemGroupMapper;
import rc.work.module.bpm.service.oa.BpmOALeaveService;
import rc.work.module.bpm.service.problem.ProblemService;
import rc.work.module.bpm.utils.SnowflakeIdGenerator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static rc.work.framework.common.pojo.CommonResult.success;
import static rc.work.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "问题整改")
@RestController
@RequestMapping("/bpm/problem")
@Validated
public class ProblemController {

    @Resource
    private ProblemService problemService;


    @Resource
    private ProblemGroupMapper problemGroupMapper;
    @Resource
    private ProblemBpmMapper problemBpmMapper;

    @Resource
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @PostMapping("/appro")
    @Operation(summary = "通过")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:create')")
    public CommonResult<Boolean> appro(@RequestParam("insId") @NotNull(message = "流程实例不能为空") String insId) {
        return success(problemService.appro(insId));
    }

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:create')")
    public CommonResult<Long> createProblem(@Valid @RequestBody ProblemSaveVO createReqVO) {
        return success(problemService.createProblem(createReqVO));
    }

    @GetMapping("/getById")
    @Operation(summary = "根据流程表id查询待处理的问题整改信息")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:query')")
    public CommonResult getById(@NotNull(message = "流程表信息不能为空") Long id) {
        LambdaQueryWrapper<ProblemGroupDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProblemGroupDO::getProblemBpmId, id);
        queryWrapper.eq(ProblemGroupDO::getStatus, 1);
        List<ProblemGroupDO> problemGroupDO = problemGroupMapper.selectList(queryWrapper);
        return success(problemGroupDO);
    }

    @GetMapping("/getByIdForSh")
    @Operation(summary = "根据流程表id查询已处理的问题整改信息")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:query')")
    public CommonResult getByIdForSh(@NotNull(message = "流程表信息不能为空") Long id) {
        LambdaQueryWrapper<ProblemGroupDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProblemGroupDO::getProblemBpmId, id);
        queryWrapper.eq(ProblemGroupDO::getStatus, 2);
        List<ProblemGroupDO> problemGroupDO = problemGroupMapper.selectList(queryWrapper);
        return success(problemGroupDO);
    }



    @GetMapping("/getBpmHistory")
    @Operation(summary = "根据groupId查询问题整改历史信息")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:query')")
    public CommonResult getBpmHistory(@NotNull(message = "根据groupId查询问题整改历史信息") String groupId) {
        List<ProblemBpmRespVO> history = problemBpmMapper.getHistory(groupId);

        return success(history);
    }



    @GetMapping("/getBpmHistoryForGraph")
    @Operation(summary = "根据groupId查询问题整改历史信息")
    @PreAuthorize("@ss.hasPermission('bpm:oa-leave:query')")
    public CommonResult getBpmHistoryForGraph(@NotNull(message = "根据groupId查询问题整改历史信息") String groupId) {
        Map<String, Object> map = problemService.getBpmHistoryForGraph(groupId);

        return success(map);
    }
    
    
    

}
